package net.cyue.web.easyquery.provider.http.router.solon;

import net.cyue.web.easyquery.core.http.HTTPRequestMethod;
import net.cyue.web.easyquery.core.http.adapter.AbstractHTTPRouter;
import net.cyue.web.easyquery.core.http.handler.api.IWebRequestHandler;
import org.noear.solon.SolonApp;
import org.noear.solon.core.handle.Handler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;

public class SolonHTTPRouter extends AbstractHTTPRouter<SolonApp> {

    private final Logger logger = LoggerFactory.getLogger(this.toString());

    /**
     * 构造函数
     *
     * @param solonApp 原始服务上下文实例
     */
    public SolonHTTPRouter(SolonApp solonApp) {
        super(solonApp);
    }

    @Override
    public boolean addRoute(String path, IWebRequestHandler handler, HTTPRequestMethod[] methods) {
        for (HTTPRequestMethod method : methods) {
            Handler solonHandler = (ctx) -> {
                handler.handle(
                    new SolonHTTPRequest(ctx),
                    new SolonHTTPResponse(ctx)
                );
            };
            switch (method) {
                case GET:
                    this.context.get(path, solonHandler);
                    break;
                case POST:
                    this.context.post(path, solonHandler);
                    break;
                case PUT:
                    this.context.put(path, solonHandler);
                    break;
                case DELETE:
                    this.context.delete(path, solonHandler);
                    break;
                case PATCH:
                    this.context.patch(path, solonHandler);
                    break;
            }
        }
        this.logger.info(
            "\n添加路由：\n\t路由: {}\n\t请求方法: {}",
            path,
            Arrays.toString(methods)
        );
        return true;
    }
}
